package com.iuhao.service;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.iuhao.rabbitMQ.RabbitMQProperties;
import com.iuhao.rabbitMQ.RabbitMessage;
import com.iuhao.rabbitMQ.config.WldLogConfig;
import com.iuhao.rabbitMQ.config.WldScheduleTaskConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by yanhao on 2017/12/2.
 */
@Service
public class RabbitMessageSendService {

    private static final Logger logger = LoggerFactory.getLogger(RabbitMessageSendService.class);

    @Autowired
    RabbitTemplate rabbitTemplate;

    @Autowired
    RabbitMQProperties properties;

    public void sendScheduleMessage(String taskType, String content) {
        try {
            RabbitMessage rabbitMessage = new RabbitMessage(taskType, content);
            Message message = MessageBuilder.withBody(rabbitMessage.getBytes()).build();
            rabbitTemplate.send(WldScheduleTaskConfig.SCHEDULETASK_ROUTINGKEY, message);
        } catch (JsonProcessingException ex) {
            logger.error("====> WLD sendScheduleMessage: " + ex.getMessage(), ex);
        }
    }


    public void sendLogMessage(String content) {
        try {
            Message message = MessageBuilder.withBody(content.getBytes()).build();
            rabbitTemplate.send(WldLogConfig.LOG_ROUTINGKEY, message);
        } catch (Exception ex) {
            logger.error("====> WLD senLogMessage: " + ex.getMessage(), ex);
        }
    }
}
